#!/usr/bin/node

function Dog(name,enery){
  let _listeners = {};
    
  this.name = name,
  this.enery = enery;

  let timer = setInterval(()=>{
    if(this.enery > 0){
      emit('bark');
      this.enery--;          
    }else{
      clearInterval(timer);              
    }  
  },1000);

  this.on=(evt,fn)=>{
    if(typeof _listeners[evt] === 'undefined'){
      _listeners[evt] = [];
    }
    _listeners[evt].push(fn);
  };

  function emit(evt,arg){
    if(typeof(_listeners[evt])==='undefined'){
      console.error('error!');
      process.exit(1);
    }
    _listeners[evt].forEach((fn)=>{
      fn.call(this,arg);
    });
  }
}
module.exports = Dog;
